package com.excel;

import com.alibaba.excel.EasyExcel;
import com.excel.config.CustomCellWriteHandler;
import com.excel.domian.DemoData;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/4/16 10:59
 * @since JDK 1.8
 */
public class Test {

    public static void main(String[] args) {
        dynamicHeadWrite();
    }

    public static void dynamicHeadWrite() {
        String fileName = "C:\\Users\\xiong\\Desktop\\" + System.currentTimeMillis() + ".xlsx";
        EasyExcel.write(fileName)
                // 这里放入动态头
                .head(head()).sheet("模板")
                .registerWriteHandler(new CustomCellWriteHandler())
                // 当然这里数据也可以用 List<List<String>> 去传入
                .doWrite(data());
    }


    private static List<List<String>> head() {
        List<List<String>> list = new ArrayList<>();
        List<String> head0 = new ArrayList<String>();
        head0.add("字符串" + System.currentTimeMillis());
        List<String> head1 = new ArrayList<String>();
        head1.add("数字" + System.currentTimeMillis());
        List<String> head2 = new ArrayList<String>();
        head2.add("日期" + System.currentTimeMillis());
        list.add(head0);
        list.add(head1);
        list.add(head2);
        return list;
    }


    private static List<DemoData> data() {
        List<DemoData> list = new ArrayList<DemoData>();
        for (int i = 0; i < 10; i++) {
            DemoData data = new DemoData();
            data.setString("字符串" + i);
            data.setDate(new Date());
            data.setDoubleData(0.56);
            list.add(data);
        }
        return list;
    }

}
